package com.zcx.common;

import java.sql.SQLOutput;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.Executor;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public enum TestEnum {

    TEST0("AAA", "111", "test0", 1),
    TEST1("BBB", "222", "test1", 2),
    TEST2("CCC", "333", "test2", 3),
    TEST3("DDD", "444", "test3", 4),
    TEST4("EEE", "555", "test4", 5),
    TEST5("FFF", "666", "test5", 6);

    private static List<StudentBean> list = new ArrayList<>();

    static {
        list = initStudent();
    }

    private static List<StudentBean> initStudent() {
        StudentBean studentBean = new StudentBean("王五", "朱老师", 27, "博士", 100, "语文");

        studentBean.setStudentBeanList(Arrays.asList(
                studentBean,
                new StudentBean("赵一", "王老师", 27, "博士", 31, "语文"),
                new StudentBean("王二", "刘老师", 22, "本科", 32, "数学"),
                new StudentBean("钱三", "王老师", 12, "大专", 55, "英语"),
                new StudentBean("李四", "刘老师", 27, "博士", 54, "语文"),
                new StudentBean("张五", "王老师", 32, "博士", 43, "数学"),
                new StudentBean("刘六", "刘老师", 42, "本科", 65, "英语"),
                new StudentBean("黑七", "王老师", 32, "博士", 23, "语文"),
                new StudentBean("王八", "刘老师", 27, "高中", 76, "数学"),
                new StudentBean("孙九", "李老师", 23, "大专", 23, "英语"),
                new StudentBean("孙一", "王老师", 26, "博士", 87, "语文"),
                new StudentBean("付二", "李老师", 26, "初中", 34, "数学"),
                new StudentBean("孙三", "李老师", 27, "博士", 98, "英语"),
                new StudentBean("李三", "李老师", 43, "高中", 54, "语文")));
        return studentBean.getStudentBeanList();
    }

    private String code;
    private String name;
    private String desc;
    private int count;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }


    TestEnum(String code, String name, String desc, int count) {
        this.code = code;
        this.name = name;
        this.desc = desc;
        this.count = count;
    }

    public String getEnumName(String name){
        TestEnum[] testEnums = TestEnum.values();
        for (TestEnum testEnum1: testEnums) {
            if (testEnum1.getName().equals(name)){
                testEnum1.setDesc("testAAA");
                System.out.println("枚举名称为："+testEnum1.getName()+"枚举code为："+testEnum1+"枚举描述："+testEnum1.getDesc());
                return testEnum1.getCode();
            }
        }
        return null;
    }


    public static void main(String[] args) {
        TaskExecutorConfiguration taskExecutorConfiguration = new TaskExecutorConfiguration();
        Executor executor = taskExecutorConfiguration.schedulingTaskExecutor();
        for (int i = 0; i < 100; i++) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                    System.out.println("thread:"+Thread.currentThread().getName()+",time:"+sdf.format(new Date()));
                }
            });
        }


        //流遍历
        Stream<TestEnum> stream = Stream.of(TestEnum.values());
        Stream<TestEnum> stream1 = Stream.of(TestEnum.values());
        Stream<TestEnum> stream2 = Arrays.stream(TestEnum.values());
        //根据条件修改枚举参数
        stream.forEach(e->{
            if (e.getName().equals("AAA")){
                e.setDesc("你好呀，AAA");
                System.out.println(e.getName()+e.getDesc());
            }
        });

        //排序
        list.sort(new Comparator<StudentBean>() {
            @Override
            public int compare(StudentBean o1, StudentBean o2) {
                if (o1.getScout() > o2.getScout()){
                    return -1;
                }else if (o1.getScout() < o2.getScout()){
                    return 1;
                }else {
                    return 0;
                }

            }
        });

        //数字求和
        System.out.println(stream1.map(TestEnum::getCount).reduce(0,Integer::sum));
        //数字相乘
        System.out.println(stream2.map(TestEnum::getCount).reduce(1,(a,b)->a*b));
        //遍历name
        List<String> newList = list.stream().map(StudentBean::getName).sorted().limit(10).collect(Collectors.toList());
        System.out.println(newList.toString());

        List ll = new ArrayList();
        //ofElseGet获取任意的元素
        System.out.println("ll的任意一个元素为："+ll.stream().findAny().orElseGet(()->new StudentBean("九儿","悍妇",22,"小学",32,"体育")).toString());
        //ofElse获取ll的第一个元素
        System.out.println("ll的任意一个元素为："+ll.stream().findAny().orElse(new StudentBean("老八","悍匪",24,"小学",32,"体育")).toString());
        //计数
        System.out.println("ll的任意一个size为："+list.stream().count());
        //去重遍历学员名称
        List<String> nameList = list.stream().map(StudentBean::getName).distinct().skip(1 * 2).limit(3).collect(Collectors.toList());
        System.out.println("skip遍历学员名称："+nameList.toString());
        List<String> nameList1 = list.stream().map(StudentBean::getName).distinct().limit(3).collect(Collectors.toList());
        System.out.println("limit遍历学员名称："+nameList1.toString());
        //map遍历学员名称，并且对张山进行修改Collectors.joining
        String name = list.stream().map(StudentBean::getName).distinct().filter(e -> e.equals("张山")).collect(Collectors.joining("", "你好", "同学"));
        System.out.println("遍历学员名称，并且对张山进行修改："+name);
        //mapToDouble求所有成绩的最大值
        OptionalDouble optionalDoublemax = list.stream().mapToDouble(StudentBean::getScout).max();
        OptionalDouble optionalDoublemin = list.stream().mapToDouble(StudentBean::getScout).min();
        System.out.println("最大成绩为："+ optionalDoublemax.getAsDouble()+"最小成绩为："+optionalDoublemin.getAsDouble());
        //stream 过滤重新组成list，计算size
        int i = list.stream().filter(e -> e.getName().equals("王五")).collect(Collectors.toList()).size();
        System.out.println("姓名为王五的成绩有："+i);
        //按照分数排序的语文课程详情
        //groupingBy 分组
        //sorted 排序 Comparator.comparing-->指定排序规则
        Map<String, List<StudentBean>> mapStu = list.stream().collect(Collectors.groupingBy(StudentBean::getKemu));
        //Map<String,List<StudentBean>> mapStu = list.stream().collect(Collectors.groupingBy(StudentBean::getKemu));
        mapStu.get("语文").stream().sorted(Comparator.comparing(StudentBean::getScout).thenComparing(StudentBean::getAge)).forEach(e->{
            System.out.println("按照分数排序的语文课程详情："+e.toString());
        });
        //allMatch 所有元素满足 返回 true , noneMatch 所有元素不匹配返回 true
        boolean flag = list.stream().peek(e->{
            e.getKemu().equals("数学");
            String key = Optional.ofNullable(e).map(s -> s.getKemu()).orElse(e.getBan());
            System.out.println("Optional非空验证："+key);
        }).allMatch(e->e.getScout()>100);
        System.out.println("所有数学成绩大于100:"+flag);
        //anyMatch 任何元素都匹配 返回true
        boolean flag1 = list.stream().peek(e->{
            e.getKemu().equals("数学");
        }).anyMatch(e->e.getScout()>55);
        System.out.println("任何数学成绩大于55:"+flag1);
        //list转map 出现key重复会报错
//        Map<String, StudentBean> studentMap = list.stream().collect(Collectors.toMap(StudentBean::getName, Function.identity()));
       // list转map 出现key重复被覆盖
        Map<String, StudentBean> studentMap = list.stream().collect(Collectors.toMap(StudentBean::getName, stu->stu,(stu1,stu2)->stu2));
        System.out.println("list 转 map 输出为："+studentMap);
        List<String> nameList2 = studentMap.keySet().stream().collect(Collectors.toList());
        System.out.println("nameList2转输出为："+nameList2);
        //求平均年龄
        Double dd = list.stream().collect(Collectors.averagingInt(StudentBean::getAge));
        System.out.println("所有学员平均年龄为："+ dd);
        //求数学最大的成绩的学员信息，返回是个optioanl 需要过滤空orElseThrow，orElseGet
        StudentBean ss = list.stream().filter(e -> e.getKemu().equals("数学")).distinct().collect(Collectors.maxBy(Comparator.comparing(StudentBean::getScout))).orElse(new StudentBean("老八", "悍匪", 24, "小学", 32, "体育"));
        System.out.println("数学最大的成绩的学员信息为："+ss.toString());
        //求最大年龄的学员
        Integer tt = list.stream().map(StudentBean::getAge).distinct().collect(Collectors.maxBy(Integer::compareTo)).orElse(0);
        System.out.println("年龄最大的学员为："+tt);
        //求年龄之和
        Integer cc = list.stream().collect(Collectors.summingInt(StudentBean::getAge));
        System.out.println("年龄之和为："+cc);
        //顺序流转换成并行流，多线程效率高
        list.stream().parallel().findFirst();
        //学员年龄所有统计
        String ff = list.stream().collect(Collectors.summarizingInt(StudentBean::getAge)).toString();
        System.out.println("学员年龄所有统计为："+ ff);
    }
}
